package com.bojiu.lawsearch.lawsearch.controller;

import com.bojiu.lawsearch.lawsearch.form.AppWithdrawReviewForm;
import com.bojiu.lawsearch.lawsearch.model.Page;
import com.bojiu.lawsearch.lawsearch.model.Task;
import com.bojiu.lawsearch.lawsearch.model.Withdraw;
import com.bojiu.lawsearch.lawsearch.service.WithdrawService;
import com.bojiu.lawsearch.lawsearch.util.AuthorizationHandle;
import com.bojiu.lawsearch.lawsearch.util.Constances;
import com.bojiu.lawsearch.lawsearch.util.JsonResult;
import com.bojiu.lawsearch.lawsearch.util.ResultCode;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author: afdiao
 * @Date：2020/4/14 15:33
 * @Description:
 */
@Api(description = "后台用户提现管理")
@RestController
@RequestMapping("backWithdraw")
@Slf4j
public class BackWithdrawController {

    @Autowired
    AuthorizationHandle authorizationHandle;

    @Autowired
    private WithdrawService withdrawService;

    /**
     * 提现列表
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @ApiOperation(value = "提现列表", notes = "提现列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Task.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @GetMapping("list/{pageNum}/{pageSize}")
    public JsonResult list(@ApiParam(name = "pageNum", value = "当前页码", required = true) @PathVariable("pageNum") int pageNum, @ApiParam(name = "pageSize", value = "每页个数", required = true) @PathVariable("pageSize") int pageSize, HttpServletRequest request) {
        //判断用户权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
        if (!authon.getCode().equals("200")) {
            return authon;
        }
        Page<Withdraw> pageList = withdrawService.queryPage(pageNum, pageSize);
        return new JsonResult(ResultCode.SUCCESS, pageList);
    }

    /**
     * 提现审核
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "提现审核")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Task.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @PostMapping("review")
    public JsonResult review(HttpServletRequest request, @Valid @RequestBody AppWithdrawReviewForm form) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
        if (!authon.getCode().equals("200")) {
            return authon;
        }
        if (form.getState().equals(0)) {
            return withdrawService.toWx(form.getWithdrawId(), "审核通过");
        } else {
            withdrawService.reject(form.getWithdrawId(), "审核未通过");
        }
        return new JsonResult(ResultCode.SUCCESS);
    }
}
